package com.jin.Es;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class EsDemo01 {
    private RestHighLevelClient client;

    @Before
    public void init() {
        client = new RestHighLevelClient(RestClient.builder
                (new HttpHost("192.168.200.129", 9200, "http")));
    }

    @Test
    public void createIndex() throws IOException {
        //1、创建对应的请求对象
        CreateIndexRequest request = new CreateIndexRequest("user");
        //指定settings配置(可以默认)
        request.settings(Settings.builder()
                .put("index.number_of_shards", 3)
                .put("index.number_of_replicas", 1));
        request.mapping("{\n" +
                        " \"properties\": {\n" +
                        " \"id\": {\n" +
                        " \"type\": \"long\"\n" +
                        " },\n" +
                        " \"name\":{\n" +
                        " \"type\": \"keyword\"\n" +
                        " },\n" +
                        " \"age\":{\n" +
                        " \"type\": \"integer\"\n" +
                        " },\n" +
                        " \"gender\":{\n" +
                        " \"type\": \"keyword\"\n" +
                        " },\n" +
                        " \"note\":{\n" +
                        " \"type\": \"text\",\n" +
                        " \"analyzer\": \"ik_max_word\"\n" +
                        " }\n" +
                        " }\n" +
                        " }"
                , XContentType.JSON);
        //2、执行请求
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        //3、得到响应结果，解析响应结果
        System.out.println(response.isAcknowledged());

    }


    @After
    public void close() {
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
